<?php
namespace Modules\AuthAdmin\Services;
class CommonService  
{
	/**
	 * @name  获取当前域名
	 * @return string
	 */
	public function get_http_type()
	{
		$http = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
		return $http.$_SERVER['HTTP_HOST'];
	}
	/**
	 * @name  将编辑器的content的图片转换为相对路径
	 * @param  id string 内容
	 * @return string
	 */
	public function remve_pic_url($content = null){
		$con = $this->get_http_type();
		if ($con){
			//提取图片路径的src的正则表达式 并把结果存入$matches中
			preg_match_all("/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/",$content,$matches);
			$img = "";
			if(!empty($matches)) {
				//注意，上面的正则表达式说明src的值是放在数组的第三个中
				$img = $matches[1];
			}else {
				$img = "";
			}
			if (!empty($img)) {
				$patterns= array();
				$replacements = array();
				$default = config('filesystems.disks.qiniu.domains.default');
				foreach($img as $imgItem){
					if (strpos($imgItem, $default) !== false) {
						$final_imgUrl = $imgItem;
					} else {
						$final_imgUrl = str_replace($con,"",$imgItem);
					}
					$replacements[] = $final_imgUrl;
					$img_new = "/".preg_replace("/\//i","\/",$imgItem)."/";
					$patterns[] = $img_new;
				}
				//让数组按照key来排序
				ksort($patterns);
				ksort($replacements);
				//替换内容
				$vote_content = preg_replace($patterns, $replacements, $content);
				return $vote_content;
			}
		} 
		return $content;
	}
	/**
	 * @name  将编辑器的content的图片转换为绝对路径
	 * @param  id string 内容
	 * @return string
	 */
	public function replace_pic_url($content = null){
		$con = $this->get_http_type();
		if ($con){
			//提取图片路径的src的正则表达式 并把结果存入$matches中
			preg_match_all("/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/",$content,$matches);
			$img = "";
			if(!empty($matches)) {
				//注意，上面的正则表达式说明src的值是放在数组的第三个中
				$img = $matches[1];
			}else {
				$img = "";
			}
			if (!empty($img)) {
				$patterns= array();
				$replacements = array();
				$default = config('filesystems.disks.qiniu.domains.default');
				foreach($img as $imgItem){
					if (strpos($imgItem, $default) !== false) {
						$final_imgUrl = $imgItem;
					} else {
						$final_imgUrl = $con.$imgItem;
					}
					$replacements[] = $final_imgUrl;
					$img_new = "/".preg_replace("/\//i","\/",$imgItem)."/";
					$patterns[] = $img_new;
				}
				//让数组按照key来排序
				ksort($patterns);
				ksort($replacements);
				//替换内容
				$vote_content = preg_replace($patterns, $replacements, $content);
				return $vote_content;
			}
		} 
		return $content;
	}
	/**
	 * @name  生成订单号
	 */
	public function make_order_no()
	{
		$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
		$orderSn =
			$yCode[intval(date('Y')) - 2018] . strtoupper(dechex(date('m'))) . date(
				'd') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf(
				'%02d', rand(0, 99));
		return $orderSn;
	}
	/**
	 * @name  二维数组json_encode中文乱码
	 */
	public function s_json_encodes($data){
		$count = count($data);
		for($k=0;$k<$count;$k++){
			foreach($data[$k] as $key => $value){  
				$data[$k][$key] = urlencode($value);  
			}
		}
		return urldecode(json_encode($data));
	} 
	/**
	 * 传入时间戳,计算距离现在的时间
	 * @param  number $the_time 时间戳
	 * @return string     返回多少以前
	 */
	public function format_time($the_time)
	{
		$now_time = time();
		$dur = $now_time - $the_time;
		if ($dur < 0) {
			return $the_time;
		} else {
			if ($dur < 60) {
				return $dur . '秒前';
			} else {
				if ($dur < 3600) {
					return floor($dur / 60) . '分钟前';
				} else {
					if ($dur < 86400) {
						return floor($dur / 3600) . '小时前';
					} else {//昨天
						//获取今天凌晨的时间戳
						$day = strtotime(date('Y-m-d', time()));
						//获取昨天凌晨的时间戳
						$pday = strtotime(date('Y-m-d', strtotime('-1 day')));
						if ($the_time > $pday && $the_time < $day) {//是否昨天
							return $t = '昨天 ' . date('H:i', $the_time);
						} else {
							if ($dur < 172800) {
								return floor($dur / 86400) . '天前';
							} else {
								return date('Y-m-d H:i', $the_time);
							}
						}
					}
				}
			}
		}
	}
	/**
	 * @name 请求接口返回内容
	 * @param  string $url [请求的URL地址]
	 * @param  string $params [请求的参数]
	 * @param  int $ipost [是否采用POST形式]
	 * @return  string
	 */
	public function juhe_curl($url, $params = false, $ispost = 0)
	{
		$httpInfo = array();
		$ch = curl_init();

		curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
		curl_setopt($ch, CURLOPT_USERAGENT, 'JuheData');
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		if ($ispost) {
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
			curl_setopt($ch, CURLOPT_URL, $url);
		} else {
			if ($params) {  
				curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($params));
			} else {
				curl_setopt($ch, CURLOPT_URL, $url);
			}
		}
		$response = curl_exec($ch);
		if ($response === FALSE) {
			//echo "cURL Error: " . curl_error($ch);
			return false;
		}
		$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
		curl_close($ch);
		return $response;
	} 
}
